from django.db import models
from user.models import User


# Create your models here.

class BaseModel(models.Model):
    """模型抽象基类"""
    is_delete = models.BooleanField(default=False, verbose_name='删除标记')

    class Meta:
        # 说明是一个抽象模型类
        abstract = True


class College(BaseModel):
    """学院"""
    college_name = models.CharField(max_length=18)
    college_create_time = models.DateField()
    college_dean = models.ForeignKey(User, on_delete=models.CASCADE)
    college_status = models.BooleanField(default=True)
    college_words = models.CharField(max_length=128)

    class Meta:
        db_table = 'college'

    def to_json(self):
        return {
            'college_name': self.college_name,
            'college_create_time': self.college_create_time,
            'college_dean': self.college_dean,
            'college_status': self.college_status,
            'college_words': self.college_words
        }


class Course(BaseModel):
    """课程表"""
    course_name = models.CharField(max_length=99)
    course_info = models.TextField(default='', verbose_name='课程描述')

    class Meta:
        db_table = 'courses'

    def to_json(self):
        return {
            'id': self.id,
            'course_name': self.course_name,
            'course_info': self.course_info
        }


class UserInfo(BaseModel):
    """用户信息"""
    number = models.CharField(max_length=18, verbose_name='身份证号', null=True)
    age = models.CharField(max_length=16, null=True)
    address = models.CharField(max_length=128, null=True)
    parents = models.ForeignKey(User, on_delete=models.CASCADE, related_name='parent', null=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, related_name='users')
    image = models.TextField(default='', verbose_name='头像')

    class Meta:
        db_table = 'userinfo'

    def to_json(self):
        return {
            'number': self.number,
            'age': self.age,

        }


class Classes(BaseModel):
    """班级表"""
    classes_name = models.CharField(max_length=48)
    classes_status = models.BooleanField(default=True)
    colleges = models.ForeignKey(College, on_delete=models.CASCADE, related_name='colleges')
    course = models.ManyToManyField(Course, related_name='courses')
    user = models.ManyToManyField(User, related_name='classes_user', blank=True)

    class Meta:
        db_table = 'classes'

    def to_json(self):
        return {
            'classes_name': self.classes_name,
            'classes_status': self.classes_status,
            'colleges': self.colleges,
            'course': self.course
        }
